<template>
  <el-dialog :title="title" :visible="showDialog" @close="closeDialog">
    <el-form ref="refrom" label-width="100px" :model="form" :rules="rules">
      <el-form-item label="分类名称" prop="catename">
        <el-input v-model="form.catename" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="submitclick">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { detalitelistAPI, putalitelistAPI, AddeListAPI } from '@/api/cate'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        catename: ''
      },
      id: null,
      rules: {
        catename: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
      }
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑分类' : '新增分类'
    }
  },
  methods: {
    /**
     * 关闭弹窗
     */
    closeDialog() {
      this.$emit('update:showDialog', false)
      this.$refs.refrom.resetFields()
      this.form = {
        catename: ''
      }
    },
    async putcatelist(id) {
      this.id = id
      const { data: res } = await detalitelistAPI(id)
      console.log(res)
      this.form = res
    },
    /**
     * 编辑、修改
     */
    submitclick() {
      this.$refs.refrom.validate(async(flag) => {
        if (!flag) return
        if (this.form.id) {
          await putalitelistAPI(this.form)
          this.$message.success('编辑成功')
        } else {
          await AddeListAPI(this.form)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.$parent.CateList()
      })
    }
  }
}
</script>

<style>

</style>
